/**
 * Ext.ux.UploadPanel
 * 
 */

Ext.namespace("Ext.ux");
Ext.ux.UploadPanel = function(config) {
	Ext.apply(this, config);
	this.delFileId = '';
	this.initComponentUI();

	Ext.ux.UploadPanel.superclass.constructor.call(this);
};

Ext.extend(Ext.ux.UploadPanel, Ext.Panel, {
	readOnly : false,

	initComponentUI : function() {

		var store = new Ext.data.Store({
			proxy : new Ext.data.MemoryProxy([]),
			reader : new Ext.data.JsonReader({}, [{
				name : 'FILE_NAME',
				mapping : 'FILE_NAME'
			}, {
				name : 'FILE_SIZE',
				mapping : 'FILE_SIZE'
			},{
				name : 'UPLOAD_USERNAME',
				mapping : 'UPLOAD_USERNAME'
			},{
				name : 'UPLOAD_TIME',
				mapping : 'UPLOAD_TIME'
			}]),
			pruneModifiedRecords : true
		});
		store.load();

		this.grid = new Ext.grid.GridPanel({
			region : 'center',
			store : store,
			cm : new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
				header : "文件名",
				width : 300,
				sortable : true,
				dataIndex : 'FILE_NAME',
				renderer : function(v, m, rec) {
					var url = ExtJ.getActionPath(
							'upload/downloadFile.do', {
								attachId : rec.get('FILE_ID')
							});
					var result = '<a href="javascript:void(0)" onclick="ExtJ.doExport({url:\''
							+ url + '\'})">' + v + '</a>';
					return result;
				}
			}, {
				header : "文件大小(Byte)",
				width : 150,
				sortable : true,
				dataIndex : 'FILE_SIZE'
			},{
				header : "上传人",
				width : 150,
				sortable : true,
				dataIndex : 'UPLOAD_USERNAME'
			},{
				header : "上传时间",
				width : 150,
				sortable : true,
				dataIndex : 'UPLOAD_TIME'
			}]),
			viewConfig : {
				forceFit : false
			},
			collapsible : false,
			animCollapse : false,
			border : true,
			iconCls : 'icon-grid'
		});

		var tools = [{
			id : 'plus',
			qtip : '增加附件',
			handler : this.onAdd.createDelegate(this)
		}, {
			id : 'minus',
			qtip : '删除附件',
			handler : this.onDelete.createDelegate(this)
		}];
		if (this.readOnly){
			tools = [];
		}
		Ext.apply(this, {
			layout : 'border',
			tools : tools,
			items : [this.grid]
		});
	},

	onDelete : function() {
		var record = this.grid.getSelectionModel().getSelected();
		if (!record || typeof(record) == 'undefined') {
			return;
		}
		this.grid.store.remove(record);
		if (0 < this.delFileId.length)
		{
			this.delFileId += ',';
		}
		this.delFileId += record.get('FILE_ID');
	},

	onAdd : function(e, t) {
		var dialog = new Ext.ux.UploadDialog.Dialog({
			autoCreate : true,
			closable : true,
			collapsible : false,
			draggable : true,
			minWidth : 400,
			minHeight : 200,
			width : 600,
			height : 400,
			proxyDrag : true,
			resizable : true,
			modal : true,
			constraintoviewport : true,
			title : '<div style="color:red">文件上传(文件大小限制在30M以内,否则无法上传)</div>',
			url : ExtJ.getActionPath('upload/uploadFile.do',{orgId : this.orgId}),
			reset_on_hide : false,
			allow_close_on_upload : true,
			base_params : {domain:document.domain}
		});
		dialog.on('uploadsuccess', this.onComplete.createDelegate(this));
		dialog.show();
	},

	onComplete : function(dialog, filename, resp) {
		this.grid.store.add(new Ext.data.Record({
			FILE_ID : resp.FILE_ID,
			FILE_NAME : resp.FILE_NAME,
			FILE_SIZE : resp.FILE_SIZE,
			UPLOAD_USERNAME : resp.UPLOAD_USERNAME,
			UPLOAD_TIME : resp.UPLOAD_TIME
		}));
	},

	getFileIds : function() {
		var ids = [];
		// this.grid.store.commitChanges();
		var data = this.grid.store.data;
		for (var index = 0; index < data.length; index++) {
			ids[ids.length] = data.itemAt(index).get('FILE_ID');
		}
		return ids;
	},
	
	getDelFileIds : function() {
		return this.delFileId;
	},

	load : function(bizId) {
		Ext.Ajax.request({
			url : ExtJ.getActionPath('upload/listFiles.do'),
			params : {
				bizId : bizId
			},
			success : function(response) {
				var rep = Ext.decode(response.responseText);
				if (rep instanceof Array) {
					this.grid.store.removeAll();
					for (var index = 0; index < rep.length; index++) {
						this.grid.store.add(new Ext.data.Record({
							FILE_ID : rep[index].FILE_ID,
							FILE_NAME : rep[index].FILE_NAME,
							FILE_SIZE : rep[index].FILE_SIZE,
							UPLOAD_USERNAME : rep[index].UPLOAD_USERNAME,
							UPLOAD_TIME : rep[index].UPLOAD_TIME
						}));
					}
				}
			}.createDelegate(this)
		});

	},
	
	reset : function(){
		this.grid.store.removeAll();
	}
});